# 基础构建阶段 - 包含所有工具链
FROM ubuntu:22.04 AS builder

# 安装基本工具
RUN apt-get update && apt-get install -y \
    git \
    cmake \
    ninja-build \
    gcc \
    g++ \
    make \
    wget \
    unzip \
    python3 \
    python3-pip \
    openjdk-17-jdk \
    && rm -rf /var/lib/apt/lists/*

# 安装Android NDK
ARG ANDROID_NDK_VERSION=25.2.9519653
RUN wget -q https://dl.google.com/android/repository/android-ndk-r25b-linux.zip && \
    unzip -q android-ndk-r25b-linux.zip -d /opt && \
    rm android-ndk-r25b-linux.zip && \
    mv /opt/android-ndk-r25b /opt/android-ndk

# 安装Windows交叉编译工具 (MinGW)
RUN apt-get update && apt-get install -y \
    gcc-mingw-w64 \
    g++-mingw-w64 \
    && rm -rf /var/lib/apt/lists/*

# 安装macOS交叉编译工具 (osxcross)
# 注意: 实际macOS编译最好在macOS主机上进行
RUN git clone https://github.com/tpoechtrager/osxcross.git /opt/osxcross && \
    cd /opt/osxcross && \
    wget -q https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz && \
    mv MacOSX11.3.sdk.tar.xz tarballs/ && \
    UNATTENDED=1 ./build.sh

# 设置环境变量
ENV ANDROID_NDK_HOME=/opt/android-ndk
ENV PATH="/opt/osxcross/target/bin:$PATH"

# 复制构建脚本
COPY build_scripts /build_scripts
COPY src /src
COPY CMakeLists.txt /

# 设置工作目录
WORKDIR /work

# 主构建命令
CMD ["/bin/bash", "-c", "./build_scripts/build_all.sh"]